package com.sanbi.shield.server.biz.service.impl;

import com.sanbi.shield.common.bean.KeyNamePair;
import com.sanbi.shield.server.biz.mapping.UserInfoBizMapping;
import com.sanbi.shield.server.biz.service.UserInfoService;
import com.sanbi.shield.server.common.bean.bo.UserInfoBO;
import com.sanbi.shield.server.common.bean.request.UserLoginRequest;
import com.sanbi.shield.server.common.bean.request.UserSelectQueryRequest;
import com.sanbi.shield.server.common.bean.vo.ResourceTreeNodeVO;
import com.sanbi.shield.server.common.bean.vo.UserInfoPdaVO;
import com.sanbi.shield.server.common.exceptions.BizException;
import com.sanbi.shield.server.common.utils.bcrypt.BCryptPasswordEncoder;
import com.sanbi.shield.server.facade.bean.dto.UserInfoDTO;
import com.sanbi.shield.server.facade.enums.ShieldExceptionMsgEnum;
import com.sanbi.shield.server.facade.service.UserInfoRpcService;
import com.sanbi.shield.server.facade.utils.JwtUtils;
import com.sanbi.shield.server.jdbc.query.UserInfoQuery;
import com.sanbi.shield.server.repository.service.UserInfoRepository;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户信息Service实现
 *
 * @author XiaoQingqing
 * @since 2024/07/25
 */
@Service
@DubboService(version = "1.0.0", interfaceClass = UserInfoRpcService.class)
public class UserInfoServiceImpl implements UserInfoService, UserInfoRpcService {
    /**
     * 用户信息Repository
     */
    @Resource
    private UserInfoRepository userInfoRepository;
    /**
     * 用户信息业务Mapping
     */
    @Resource
    private UserInfoBizMapping userInfoBizMapping;

    @Override
    public String login(UserLoginRequest request) {
        UserInfoBO userInfo = userInfoRepository.queryByLoginName(request.getUsername());
        if(null == userInfo){
            throw new BizException(ShieldExceptionMsgEnum.LOGIN_FAILURE_EXCEPTION);
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if (!encoder.matches(request.getPassword(), userInfo.getPassword())) {
            throw new BizException(ShieldExceptionMsgEnum.LOGIN_FAILURE_EXCEPTION);
        }
        return JwtUtils.encodeDefault(userInfo.getId().toString(), 12 * 60 * 60);
    }

    @Override
    public UserInfoPdaVO queryPdaUserInfo(Long userId) {
        UserInfoBO userInfo = userInfoRepository.queryById(userId);
        if(null == userInfo){
            return null;
        }
        return new UserInfoPdaVO().setId(userInfo.getId()).setUsername(userInfo.getLoginName()).setRealName(userInfo.getRealName());
    }

    @Override
    public List<UserInfoDTO> queryById(List<Long> userIds) {
        List<UserInfoBO> userInfoList = userInfoRepository.queryById(userIds);
        return userInfoBizMapping.toDTO(userInfoList);
    }

    @Override
    public List<KeyNamePair<Long, String>> querySelectUser(UserSelectQueryRequest request) {
        UserInfoQuery query = new UserInfoQuery();
        query.setRealName(request.getRealName());
        List<UserInfoBO> userInfoList = userInfoRepository.queryList(query);
        return userInfoList.stream().map(e -> new KeyNamePair<>(e.getId(), e.getRealName())).toList();
    }

    @Override
    public List<ResourceTreeNodeVO> queryPermTree(){
        List<ResourceTreeNodeVO> topNodeList = new ArrayList<>();
        ResourceTreeNodeVO warehouseInNode = new ResourceTreeNodeVO();
        topNodeList.add(warehouseInNode);
        warehouseInNode.setId(1L);
        warehouseInNode.setPid(0L);
        warehouseInNode.setName("入库");
        warehouseInNode.setKey("WarehouseIn");
        warehouseInNode.setIconUrl("");
        List<ResourceTreeNodeVO> warehouseInNodeChildren = new ArrayList<>();
        ResourceTreeNodeVO warehouseInByBoxNode = new ResourceTreeNodeVO();
        warehouseInNodeChildren.add(warehouseInByBoxNode);
        warehouseInByBoxNode.setId(2L);
        warehouseInByBoxNode.setPid(1L);
        warehouseInByBoxNode.setName("整箱入库");
        warehouseInByBoxNode.setKey("WholeBoxStorage");
        warehouseInByBoxNode.setIconUrl("warehouse_in_whole_box_icon");
        ResourceTreeNodeVO warehouseInByPcsNode = new ResourceTreeNodeVO();
        warehouseInNodeChildren.add(warehouseInByPcsNode);
        warehouseInByPcsNode.setId(3L);
        warehouseInByPcsNode.setPid(1L);
        warehouseInByPcsNode.setName("单件入库");
        warehouseInByPcsNode.setKey("SinglePieceStorage");
        warehouseInByPcsNode.setIconUrl("single_piece_storage_icon");
        warehouseInNode.setChildren(warehouseInNodeChildren);
        ResourceTreeNodeVO warehouseOutNode = new ResourceTreeNodeVO();
        topNodeList.add(warehouseOutNode);
        warehouseOutNode.setId(4L);
        warehouseOutNode.setPid(0L);
        warehouseOutNode.setName("出库");
        warehouseOutNode.setKey("WarehouseOut");
        warehouseOutNode.setIconUrl("");
        List<ResourceTreeNodeVO> warehouseOutNodeChildren = new ArrayList<>();
        ResourceTreeNodeVO warehouseOutByBoxNode = new ResourceTreeNodeVO();
        warehouseOutNodeChildren.add(warehouseOutByBoxNode);
        warehouseOutByBoxNode.setId(5L);
        warehouseOutByBoxNode.setPid(4L);
        warehouseOutByBoxNode.setName("整箱出库");
        warehouseOutByBoxNode.setKey("WholeBoxOutOfWarehouse");
        warehouseOutByBoxNode.setIconUrl("out_of_warehouse_whole_box_icon");
        ResourceTreeNodeVO warehouseOutByPcsNode = new ResourceTreeNodeVO();
        warehouseOutNodeChildren.add(warehouseOutByPcsNode);
        warehouseOutByPcsNode.setId(6L);
        warehouseOutByPcsNode.setPid(4L);
        warehouseOutByPcsNode.setName("单件出库");
        warehouseOutByPcsNode.setKey("SinglePieceOutOfWarehouse");
        warehouseOutByPcsNode.setIconUrl("out_of_warehouse_single_piece_icon");
        warehouseOutNode.setChildren(warehouseOutNodeChildren);
        ResourceTreeNodeVO warehousePickNode = new ResourceTreeNodeVO();
        topNodeList.add(warehousePickNode);
        warehousePickNode.setId(7L);
        warehousePickNode.setPid(0L);
        warehousePickNode.setName("拣货");
        warehousePickNode.setKey("WarehousePick");
        warehousePickNode.setIconUrl("");
        List<ResourceTreeNodeVO> warehousePickNodeChildren = new ArrayList<>();
        ResourceTreeNodeVO warehousePickByBoxNode = new ResourceTreeNodeVO();
        warehousePickNodeChildren.add(warehousePickByBoxNode);
        warehousePickByBoxNode.setId(8L);
        warehousePickByBoxNode.setPid(7L);
        warehousePickByBoxNode.setName("整箱拣货");
        warehousePickByBoxNode.setKey("WholeBoxPickUpOuter");
        warehousePickByBoxNode.setIconUrl("pick_up_outer_whole_box_icon");
        ResourceTreeNodeVO warehousePickByPcsNode = new ResourceTreeNodeVO();
        warehousePickNodeChildren.add(warehousePickByPcsNode);
        warehousePickByPcsNode.setId(9L);
        warehousePickByPcsNode.setPid(7L);
        warehousePickByPcsNode.setName("单件拣货");
        warehousePickByPcsNode.setKey("SinglePiecePickUpOuter");
        warehousePickByPcsNode.setIconUrl("pick_up_outer_single_piece_icon");
        warehousePickNode.setChildren(warehousePickNodeChildren);
        ResourceTreeNodeVO ShelfMoveNode = new ResourceTreeNodeVO();
        topNodeList.add(ShelfMoveNode);
        ShelfMoveNode.setId(10L);
        ShelfMoveNode.setPid(0L);
        ShelfMoveNode.setName("移位");
        ShelfMoveNode.setKey("ShelfMove");
        ShelfMoveNode.setIconUrl("");
        List<ResourceTreeNodeVO> ShelfMoveNodeChildren = new ArrayList<>();
        ResourceTreeNodeVO ShelfMoveByBoxNode = new ResourceTreeNodeVO();
        ShelfMoveNodeChildren.add(ShelfMoveByBoxNode);
        ShelfMoveByBoxNode.setId(11L);
        ShelfMoveByBoxNode.setPid(10L);
        ShelfMoveByBoxNode.setName("整箱移位");
        ShelfMoveByBoxNode.setKey("WholeBoxShifting");
        ShelfMoveByBoxNode.setIconUrl("whole_box_shifting_icon");
        ResourceTreeNodeVO ShelfMoveByPcsNode = new ResourceTreeNodeVO();
        ShelfMoveNodeChildren.add(ShelfMoveByPcsNode);
        ShelfMoveByPcsNode.setId(12L);
        ShelfMoveByPcsNode.setPid(10L);
        ShelfMoveByPcsNode.setName("单件移位");
        ShelfMoveByPcsNode.setKey("SinglePieceShift");
        ShelfMoveByPcsNode.setIconUrl("single_piece_shifting_icon");
        ShelfMoveNode.setChildren(ShelfMoveNodeChildren);
        ResourceTreeNodeVO acceptanceNode = new ResourceTreeNodeVO();
        topNodeList.add(acceptanceNode);
        acceptanceNode.setId(13L);
        acceptanceNode.setPid(0L);
        acceptanceNode.setName("验收");
        acceptanceNode.setKey("Acceptance");
        acceptanceNode.setIconUrl("");
        List<ResourceTreeNodeVO> acceptanceNodeChildren = new ArrayList<>();
        ResourceTreeNodeVO acceptanceByBoxNode = new ResourceTreeNodeVO();
        acceptanceNodeChildren.add(acceptanceByBoxNode);
        acceptanceByBoxNode.setId(14L);
        acceptanceByBoxNode.setPid(13L);
        acceptanceByBoxNode.setName("整箱验收");
        acceptanceByBoxNode.setKey("WholeBoxAcceptance");
        acceptanceByBoxNode.setIconUrl("whole_box_acceptance_icon");
        acceptanceNode.setChildren(acceptanceNodeChildren);
        return topNodeList;
    }

}
